Skip to content

Reduce specializations - #1607

Merged
awni merged 13 commits into
mainfrom
reduce_specializations
Nov 22, 2024
Merged

awni merged 13 commits into
mainfrom
reduce_specializations

Conversation

@awni

@awni awni commented Nov 20, 2024 •

Copy link
Copy Markdown
Member
  • Adds uint specialization for indexing
  • Changes looped indexer to be faster for more indices
  • Reduces the binary size by:
    • uint and int use the same kernels for sum and prod
    • and and or use the same kernel for all dtypes with the same size
    • Removed unneeded (DIM=0 specialization in the looper)
    • Using three dimension specializations DIM=1,2,5
  • Changed sum and prod to reduce integer types into 32-bit precision. This follows Jax / NumPy and I think is more consistent given that we already do this for bool.

Binary size

Old Metal library size = 77 Mb
New Metal library size = 76 Mb

Adding the same fusion of operations to the CPU dispatch saves about 0.7 MB from the CPU-only build:

Pre: 11.6 MB
Post: 10.9 MB

Benchmarks M1 MAX

Size Axes Transposed Pre Post
256x128x128x16 0,1,2 N 8.89 8.09
2x4x4x4194304 0,2 N 23.96 18.9
128x512x128 1 N 7.17 2.51
4x128x256x1024 0,2 N 13.29 13.25
512x512x64 2 Y 25.85 27.07
512x4x4x4x4x4x4x4x4 0,2,4,6,8 N 7.13 6.41
32x4x4x4x4x4x4x4x4x4x4 0,2,4,6,8,10 N 59.35 4.91
4x4x4x4x4x4x4x4x4x128 1,3,5,7,9 Y 3.43 3.27
4x32x4x32x128 0,2,4 N 0.202 0.178
4x8x4x8x4x128 1,3,5 N 0.110 0.104
4x8x4x8x4x32x32 0,2,4,6 N 0.626 0.646
4x8x4x8x4x32x16x2 0,2,4,6 N 46.22 10.77

Benchmarks M3 MAX

Size Axes Transposed Pre Post
256x128x128x16 0,1,2 N 6.72 6.71
2x4x4x4194304 0,2 N 15.75 15.82
128x512x128 1 N 3.32 1.22
4x128x256x1024 0,2 N 13.22 13.16
512x512x64 2 Y 1.49 1.57
512x4x4x4x4x4x4x4x4 0,2,4,6,8 N 8.352 5.98
32x4x4x4x4x4x4x4x4x4x4 0,2,4,6,8,10 N 29.50 8.70
4x4x4x4x4x4x4x4x4x128 1,3,5,7,9 Y 2.84 3.06
4x32x4x32x128 0,2,4 N 0.108 0.103
4x8x4x8x4x128 1,3,5 N 0.081 0.075
4x8x4x8x4x32x32 0,2,4,6 N 0.22 0.21
4x8x4x8x4x32x16x2 0,2,4,6 N 27.40 5.03

Full benchmarks below:

#!/bin/bash
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 256x128x128x16 --axis 0,1,2
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 2x4x4x4194304 --axis 0,2
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 128x512x128 --axis 1
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 4x128x256x1024 --axis 0,2
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 512x512x64 --transpose 1,0,2 --axis 2
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 512x4x4x4x4x4x4x4x4 --axis 0,2,4,6,8
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 32x4x4x4x4x4x4x4x4x4x4 --axis 0,2,4,6,8,10
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 4x4x4x4x4x4x4x4x4x128 --transpose 1,0,2,3,4,5,6,7,8,9 --axis 1,3,5,7,9
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 4x32x4x32x128 --axis 0,2,4
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 4x8x4x8x4x128 --axis 1,3,5
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 4x8x4x8x4x32x32 --axis 0,2,4,6
python benchmarks/python/comparative/bench_mlx.py sum_axis --size 4x8x4x8x4x32x16x2 --axis 0,2,4,6

@awni awni changed the title [WIP] Reduce specializations Reduce specializations Nov 21, 2024
@awni
awni marked this pull request as ready for review November 21, 2024 01:14
@awni
awni requested a review from angeloskath November 21, 2024 01:14
@awni

awni commented Nov 21, 2024

Copy link
Copy Markdown
Member Author

I think this is in a good enough state for review. The one benchmark that is causing issues is:

python benchmarks/python/comparative/bench_mlx.py sum_axis --size 512x512x64 --transpose 1,0,2 --axis 2

Notice it's really slow on M1 compared to M3 and also I observe faster in size_t than uint which is very unusual (!!). I suspect fixing the source of the slowness for that will cause the benchmark to look better in uint.

@angeloskath
angeloskath force-pushed the reduce_specializations branch from 72baace to 6d5c120 Compare November 22, 2024 03:17

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

I 'll get to fixing the small rows in a separate PR. I will look into outputting strided matrices such that the reduction is always contiguous.

@awni
awni merged commit 0c5eea2 into main Nov 22, 2024
@awni
awni deleted the reduce_specializations branch November 22, 2024 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants